<?php


namespace app\controller;


use think\facade\Db;

class PickerOptions
{
    public function gong_huo_dan_wei()
    {
        // $list = Db::table("tabghdw")->select();
        $list = [];
        // 改为根据用户身份写死
        $req = Request();
        $openid = $req->openid;
        // openid 查 Sys_WeChatInf 关联到account  
        $res = Db::table("Sys_WeChatInf")->where("wx_h5_openid", $openid)->find();
        if (!$res) {
            return json([
                'code' => -1,
                'msg' => '未查到此微信用户'
            ]);
        }
        $Account = $res['Account'];
        // 再用account 查 JL_CZKeHuJL 获得用户
        $kehu = Db::table("JL_CZKeHuJL")->where("Account", $Account)->find();
        if (!$kehu) {
            return json([
                'code' => -1,
                'msg' => '未查到此客户记录'
            ]);
        }

        $kehu_type = $kehu['type'];
        // 销售客户
        if ($kehu_type == '0') {
            $list = [
                [
                    '单位名称' => '新乡市长城矿粉有限公司',
                    '编码' => ''
                ]
            ];
        }
        // 供应商
        if ($kehu_type == '1') {
            $list = [
                [
                    '单位名称' => $kehu['Name'],
                    '编码' => ''
                ]
            ];
        }


        return json([
            'code' => 1,
            'msg' => 'ok',
            'data' => $list
        ]);
    }

    // 产地
    public function chan_di()
    {
        // $list = Db::table("JL_CZChanDi")->select();

        $list = [];
        // 改为根据用户身份写死
        $req = Request();
        $openid = $req->openid;
        // openid 查 Sys_WeChatInf 关联到account  
        $res = Db::table("Sys_WeChatInf")->where("wx_h5_openid", $openid)->find();
        if (!$res) {
            return json([
                'code' => -1,
                'msg' => '未查到此微信用户'
            ]);
        }
        $Account = $res['Account'];
        // 再用account 查 JL_CZKeHuJL 获得用户
        $kehu = Db::table("JL_CZKeHuJL")->where("Account", $Account)->find();
        if (!$kehu) {
            return json([
                'code' => -1,
                'msg' => '未查到此客户记录'
            ]);
        }
        $kehu_code = $kehu['BianHao'];
        $huowucode = input("huowucode");
        $chandi = Db::table("JL_CZKeHuHuoWuChanDi")->where([
            'KeHuCode' => $kehu_code,
            'HWCode' => $huowucode
        ])->find();
         

        if (!$chandi) {
            return json([
                'code' => -1,
                'msg' => '未查到相关产地信息'
            ]);
        }

        $chandi_codes = $chandi['ChanDiCode'];
        $list = Db::table("JL_CZChanDi")->where('Code','IN',$chandi_codes)->select()->toArray();


        return json([
            'code' => 1,
            'msg' => 'ok',
            'data' => $list
        ]);
    }

    public function shou_huo_dan_wei()
    {
        // $list = Db::table("tabshdw")->select();

        // return json([
        //     'code'=>1,
        //     'msg'=>'ok',
        //     'data'=>$list
        // ]);
        // $list = Db::table("tabghdw")->select();
        $list = [];
        // 改为根据用户身份写死
        $req = Request();
        $openid = $req->openid;
        // openid 查 Sys_WeChatInf 关联到account  
        $res = Db::table("Sys_WeChatInf")->where("wx_h5_openid", $openid)->find();
        if (!$res) {
            return json([
                'code' => -1,
                'msg' => '未查到此微信用户'
            ]);
        }
        $Account = $res['Account'];
        // 再用account 查 JL_CZKeHuJL 获得用户
        $kehu = Db::table("JL_CZKeHuJL")->where("Account", $Account)->find();
        if (!$kehu) {
            return json([
                'code' => -1,
                'msg' => '未查到此客户记录'
            ]);
        }

        $kehu_type = $kehu['type'];
        // 销售客户
        if ($kehu_type == '0') {
            $list = [
                [
                    '单位名称' => $kehu['Name'],
                    '编码' => ''
                ]
            ];
        }
        // 供应商
        if ($kehu_type == '1') {

            $list = [
                [
                    '单位名称' => '新乡市长城矿粉有限公司',
                    '编码' => ''
                ]
            ];
        }


        return json([
            'code' => 1,
            'msg' => 'ok',
            'data' => $list
        ]);
    }

    /*
        货物名称 选项
    */
    public function huo_wu_ming_cheng()
    {
        $req = Request();
        $openid = $req->openid;
        // openid 查 Sys_WeChatInf 关联到account  
        $res = Db::table("Sys_WeChatInf")->where("wx_h5_openid", $openid)->find();
        if (!$res) {
            return json([
                'code' => -1,
                'msg' => '未查到此微信用户'
            ]);
        }
        $Account = $res['Account'];
        // 再用account 查 JL_CZKeHuJL 获得用户
        $kehu = Db::table("JL_CZKeHuJL")->where("Account", $Account)->find();
        if (!$kehu) {
            return json([
                'code' => -1,
                'msg' => '未查到此客户记录'
            ]);
        }
        $kehu_type = $kehu['type'];

        // 销售客户
        if ($kehu_type == '0') {
            // 销售客户 直接 查询 tabhwmc表 type=0的

            $list = Db::table("tabhwmc2")->where('type', '0')->select();
            return json([
                'code' => 1,
                'msg' => 'ok',
                'data' => $list
            ]);
        }
        // 供应商
        if ($kehu_type == '1') {

            // 供应商 取 HWcode 字段 筛选查询 tabhwmc表
            // tabhwmc表 对应筛选条件 为 “编码”字段
            $HWCode = $kehu['HWCode'];
            $HWcodeArray = explode(",", $HWCode);

            $list = Db::table("tabhwmc2")->where('code', "IN", $HWcodeArray)->select();
            return json([
                'code' => 1,
                'msg' => 'ok',
                'data' => $list
            ]);
        }


    }
}